//
//  SHLendingView.swift
//  SuperCash
//
//  Created by Daiyi on 2024/1/14.
//

import UIKit
import DDBase
import KPAppKit

class KPLendingView: UIView {
    typealias KPLendingAction = ((_ loanView: KPLendingView?) -> Void)
    var action: KPLendingAction? = nil
    private weak var proInfoBtn: UIButton?
    private weak var amountLabel: UILabel?

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 内容
        self.backgroundColor = .white
        self.dd.makeRadius(10)
        
        // 背景
        let waitImage = UIImageView(image: UIImage(hNamed: "ic_home_wait")).ddChain
            .setY(-10)
            .setMidX(self.width * 0.5)
            .build()
        self.addSubview(waitImage)
        
        let margin: CGFloat = 8
        let leftTitle = UILabel(frame: CGRect(x: margin, y: 70, width: self.width - 2 * margin, height: 20)).ddChain
            .textColor(.rgb(value: 0, a: 0.9))
            .font(.semibold(16))
            .textAlign(.center)
            .text("El banco está procesando el pago")
            .build()
        self.addSubview(leftTitle)
        
        let descLabel = UILabel(frame: CGRect(x: margin, y: leftTitle.bottom + 6, width: leftTitle.width, height: 48))
            .ddChain
            .font(.font12)
            .textColor(.rgb(value: 0, a: 0.6))
            .numberOfLines(0)
            .text("Sujeto al horario laboral del banco, si se encuentra en un periodo de descanso del banco, el tiempo de llegada del pago se retrasará. Por favor, revise su banca móvil.")
            .sizeToFitHeight()
            .build()
        self.addSubview(descLabel)
        
        let proInfoBtn = UIButton(type: .custom).ddChain
            .frame(CGRect(x: self.width - 98, y: 20, width: 98, height: 36))
            .font(.font12)
            .setTitleColor(.black)
            .setTitle("Kawan")
            .setImage(UIImage(hNamed: "ic_home_pro_logo01"))
            .contentInset(UIEdgeInsets(top: 0, left: 6, bottom: 0, right: 6))
            .build()
        proInfoBtn.titleLabel?.numberOfLines = 0
        proInfoBtn.contentHorizontalAlignment = .left
        proInfoBtn.layer.backgroundColor = UIColor.hex(hexString: "#EFEFEF").cgColor
        self.addSubview(proInfoBtn)
        self.proInfoBtn = proInfoBtn
        
        let appColor = DDAppearance.shared.appTinColor
        let amountBg = UIView(frame: CGRect(x: 20, y: descLabel.bottom + 12, width: self.width - 40, height: 70)).ddChain.backgroundColor(appColor.withAlphaComponent(0.6))
            .cornerRadius(10)
            .build()
        self.addSubview(amountBg)
        
        let amountLabel = UILabel(frame: CGRect(x: 10, y: 0, width: amountBg.width - 20, height: 26)).ddChain
            .font(.font12)
            .textColor(.rgb(value: 0, a: 0.6))
            .textAlign(.center)
            .build()
        amountBg.addSubview(amountLabel)
        self.amountLabel = amountLabel
    
        let loanBtn = UIButton(type: .custom).ddChain
            .frame(CGRect(x: 0, y: amountLabel.bottom, width: amountBg.width, height: 44))
            .setTitleColor(.white)
            .font(.semibold(14))
            .setBackgroundImgColor(appColor, radius: 10)
//            .contentInset(UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10))
            .setTitle("Acelerar la recepción del pago/Ya recibido")
            .addTarget(self, action: #selector(goLoan))
            .build()
        loanBtn.titleLabel?.adjustsFontSizeToFitWidth = true
        amountBg.addSubview(loanBtn)
        
        self.setModel(nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    convenience init(frame: CGRect, action: KPLendingAction?) {
        self.init(frame: frame)
        self.action = action
    }
    
    @objc private func goLoan(_ sender: UIButton) {
        self.action?(self)
    }
    
    func setModel(_ model: KPOrderModel?) {
        var amount: String = "0"
        if let model = model {
            var proType = max(model.productType, 1)
            proType = proType > 7 ? 1 : proType
            self.proInfoBtn?.setImage(UIImage(hNamed: "ic_home_pro_logo0\(proType)"), for: .normal)
            let name = "\(model.productName.replacingOccurrences(of: " ", with: "\n"))"
            self.proInfoBtn?.setTitle(name, for: .normal)
            
            amount = model.nextUpAmount
        } else {
            self.proInfoBtn?.setImage(UIImage(hNamed: "ic_home_pro_logo01"), for: .normal)
//            let name = "Kawan\nPrestamo"
//            self.proInfoBtn?.setTitle(name, for: .normal)
        }
        
        let attr = NSMutableAttributedString(attributedString: self.upAmountAttr)
        attr.append(NSAttributedString(string: "$\(amount)", attributes: [.font: UIFont.bold(12), .foregroundColor: DDAppearance.shared.appTinColor]))
        self.amountLabel?.attributedText = attr
    }
    
    private lazy var upAmountAttr: NSAttributedString = {
        let str = NSAttributedString(string: "El importe restante a devolver: ", attributes: [.font: UIFont.bold(12), .foregroundColor: UIColor.rgb(value: 0, a: 0.6)])
        return str
    }()
}

